package com.liinxin.flac_management.controller;

import cn.hutool.core.io.FileUtil;
import com.liinxin.flac_management.utils.FileManage;
import com.liinxin.flac_management.utils.TxtManage;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Collectors;

public class TxtMatchController {

    public String textMatch(String libPath, String txtPath) {
        // 获取已有的文本库
        File lib = FileUtil.file(FileManage.correctPath(libPath));

        // 获取新添加的文本
        File txt = FileUtil.file(FileManage.correctPath(txtPath));

        // 将内容转换为字符串
        List<String> libList = TxtManage.txtContentToList(lib);
        List<String> txtList = TxtManage.txtContentToList(txt);

        System.out.println("文本库: " + libList);
        System.out.println("新文本: " + txtList);

        // 添加到set集合
        Set<String> libSet = new HashSet<>();
        libList.stream().forEach(s -> libSet.add(s));
        // System.out.println(libSet);

        // 重复项集合
        List<String> repeatList = txtList.stream().filter(s -> libSet.contains(s)).collect(Collectors.toList());
        // System.out.println("新文本_重复的文本: " + repeatList);

        // 不重复项集合
        List<String> noRepeatList = txtList.stream().filter(s -> !libSet.contains(s)).collect(Collectors.toList());
        // System.out.println("新文本_新添加的文本: " + noRepeatList);

        StringJoiner sj = new StringJoiner("\r\n\r\n", "", "");

        for (int i = 0; i < noRepeatList.size(); i++) {
            sj.add(noRepeatList.get(i));
        }
        // System.out.println(sj);
        return sj.toString();
    }

    public String duplicates(String txtPath) {
        // 获取新添加的文本
        File txt = FileUtil.file(FileManage.correctPath(txtPath));

        // 将内容转换为字符串
        List<String> txtList = TxtManage.txtContentToList(txt);

        // 输出文本
        System.out.println("新文本: " + txtList);

        // 去重, 加入set集合
        Set<String> txtSet = new HashSet<>();
        txtList.stream().forEach(s -> txtSet.add(s));

        System.out.println("去重后的文本: " + txtSet);

        StringJoiner sj = new StringJoiner("\r\n\r\n", "", "");

        for (String s : txtSet) {
            sj.add(s);
        }
        // System.out.println(sj);
        return sj.toString();
    }
}
